package com.atguigu.spzx.manager.helper;

import com.atguigu.spzx.model.entity.system.SysMenu;

import java.util.LinkedList;
import java.util.List;

/**
 * @author MikeCoder
 * @create 2023-12-0615:44
 * @description:
 * @verson:
 */
public class MenuHelper {
    /**
     * 使用递归方法建菜单
     */
    public static List<SysMenu> buildTree(List<SysMenu> menuList){
        List<SysMenu> menusTree = new LinkedList<>();
        for (SysMenu sysMenu : menuList) {
            if(sysMenu.getParentId().longValue() == 0){
                menusTree.add(findChildren(sysMenu,menuList));
            }
        }

        return menusTree;
    }

    private static SysMenu findChildren(SysMenu sysMenu,List<SysMenu> menuList){
        if(sysMenu.getChildren() == null){
            sysMenu.setChildren(new LinkedList<SysMenu>());
        }
        for (SysMenu menu : menuList) {
            if(sysMenu.getId().longValue() == menu.getParentId().longValue()){
                sysMenu.getChildren().add(findChildren(menu,menuList));
            }
        }
        return sysMenu;
    }
}
